package com.lineage.server.serverpackets;

import com.lineage.server.datatables.lock.AuctionBoardReading;
import com.lineage.server.model.Instance.L1NpcInstance;
import com.lineage.server.templates.L1AuctionBoardTmp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Iterator;

public class S_AuctionBoard extends ServerBasePacket {
  private byte[] _byte;
  
  public S_AuctionBoard(L1NpcInstance board) {
    this._byte = null;
    buildPacket(board);
  }
  
  private void buildPacket(L1NpcInstance board) {
    ArrayList<L1AuctionBoardTmp> houseListX = new ArrayList<>();
    Collection<L1AuctionBoardTmp> boardList = AuctionBoardReading.get().getAuctionBoardTableList().values();
    Iterator<L1AuctionBoardTmp> iterator = boardList.iterator();
    while (iterator.hasNext()) {
      L1AuctionBoardTmp boardX = iterator.next();
      int houseId = boardX.getHouseId();
      if (board.getX() == 33421 && board.getY() == 32823) {
        if (houseId < 262145 || houseId > 262189)
          continue; 
        houseListX.add(boardX);
        continue;
      } 
      if (board.getX() == 33585 && board.getY() == 33235) {
        if (houseId < 327681 || houseId > 327691)
          continue; 
        houseListX.add(boardX);
        continue;
      } 
      if (board.getX() == 33959 && board.getY() == 33253) {
        if (houseId < 458753 || houseId > 458819)
          continue; 
        houseListX.add(boardX);
        continue;
      } 
      if (board.getX() != 32611 || board.getY() != 32775 || houseId < 524289 || houseId > 524294)
        continue; 
      houseListX.add(boardX);
    } 
    writeC(156);
    writeD(board.getId());
    writeH(houseListX.size());
    Iterator<L1AuctionBoardTmp> iterator2 = houseListX.iterator();
    while (iterator2.hasNext()) {
      L1AuctionBoardTmp boardX = iterator2.next();
      writeD(boardX.getHouseId());
      writeS(boardX.getHouseName());
      writeH(boardX.getHouseArea());
      Calendar cal = boardX.getDeadline();
      writeC(cal.get(2) + 1);
      writeC(cal.get(5));
      writeD((int)boardX.getPrice());
    } 
  }
  
  public byte[] getContent() {
    if (this._byte == null)
      this._byte = getBytes(); 
    return this._byte;
  }
  
  public String getType() {
    return getClass().getSimpleName();
  }
}
